package org.subside.Play;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;

import java.awt.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * @Author : zhangbo
 * @data : 2024/6/7
 * @description :
 */
@Slf4j
public class CreateIndexPicture {
    private static final int QR_CODE_SIZE = 200;

    public static void main(String[] args) throws WriterException, IOException {
        String pathLocal = "C:/Users/86199/Desktop/开发_张博_新致软件.doc";
        String text = "Hello, World!";
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, QR_CODE_SIZE, QR_CODE_SIZE);
        Path path = FileSystems.getDefault().getPath("qrcode.png");
        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
        File file = new File(pathLocal);
        FileReader fileReader = new FileReader(file);
        if (file.exists()) {
            try {
                // 创建一个BufferedReader对象
                BufferedReader reader = new BufferedReader(fileReader);
                // 使用正确的编码格式读取文件内容，这里假设文件是UTF-8编码
                byte[] bytes = Files.readAllBytes(Paths.get(pathLocal));
                String content1 = new String(bytes, StandardCharsets.UTF_8);
                // 创建一个字符串变量用于存储文件内容
                String line;
                StringBuilder content = new StringBuilder();
                // 逐行读取文件内容
                while ((line = reader.readLine()) != null) {
                    content.append(line).append("\n");
                }
                // 输出文件内容
                System.out.println(content.toString());
                System.out.println(content1);
                FileInputStream fis = new FileInputStream(new File(pathLocal));
                XWPFDocument document = new XWPFDocument(fis);
                // 读取文档中的所有段落
                List<XWPFParagraph> paragraphs = document.getParagraphs();
                for (XWPFParagraph para : paragraphs) {
                    System.out.println(para.getText());
                }

                fis.close();
            } catch (IOException e) {
                log.info("文件不存在！" + e);
            }
            // 关闭文件
            fileReader.close();
        }
        System.out.println("二维码已生成：" + path);
    }
}
